<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="问卷名称" prop="surveyName">
          <el-input style="width: 200px" v-model="queryParams.surveyName" placeholder="请输入问卷名称" clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
  
        <el-form-item label="问卷类型" prop="surveyType">
          <el-select style="width: 200px" v-model="queryParams.surveyType" placeholder="请选择问卷类型" clearable>
            <el-option v-for="dict in dict.survey_type" :key="dict.dictValue" :label="dict.dictName" :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
  
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-table v-loading="loading" :data="surveyList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="问卷名称" prop="surveyName" />
        <el-table-column label="问卷描述" prop="surveyDesc" show-overflow-tooltip />
        <el-table-column label="问卷类型" prop="surveyType">
          <template #default="scope">
            <template v-for="item in dict.survey_type" :key="item.dictValue">
              <el-tag v-if="item.dictValue == scope.row.surveyType" :type="item.cssClass">
                {{ item.dictName }}
              </el-tag>
            </template>
          </template>
        </el-table-column>
  
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope"> 
            <el-button size="small" type="primary" text icon="View" @click="handlePreview(scope.row)">预览</el-button>
            <el-button size="small" type="primary" text icon="RefreshLeft" @click="handleRestore(scope.row)">还原</el-button>
            <el-button size="small" type="danger" text icon="Delete" @click="handleDelete(scope.row)">永久删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <el-pagination
        style="margin-top: 10px"
        size="small" 
        layout="prev, pager, next, jumper, ->, total"
        :total="total"
        :page-size="queryParams.pageSize"
        :current-page="queryParams.pageNum"
        @current-change="getList"
      />
    </div>
  </template>
  
  <script>
  import {
    listSurvey,
    delSurvey,
    restoreSurvey,
  } from "@/api/survey/survey";
  import { getDicts } from "@/api/dict";
  import { ElMessageBox } from 'element-plus'

  
  export default {
    name: "Survey",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 问卷表格数据
        surveyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          surveyName: null,
          surveyDesc: null,
          surveyType: null,
          status: "0",
          bookCode: null,
        },
        // 表单参数
        form: {
          surveyName: ''
        },
        // 表单校验
        rules: {
          surveyName: [
            { required: true, message: "问卷名称不能为空", trigger: "blur" },
          ],
        },
        dict: {
          survey_type: [],
          survey_status: [],
        },
      };
    },
    async created() {
      this.getList();
      this.dict.survey_status = await getDicts("survey_status");
      this.dict.survey_type = await getDicts("survey_type");
    },
    methods: {
      /** 查询问卷列表 */
      getList() {
        this.loading = true;
        listSurvey(this.queryParams).then((response) => {
          this.surveyList = response.records;
          this.total = response.total;
          this.loading = false;
        });
      },
      
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs.queryForm.resetFields();
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.surveyId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      
      /** 预览按钮操作 */
      handlePreview(row) {
        var routeUrl = this.$router.resolve({
          path: `/preview/${row.surveyId}`,
        });
        window.open(routeUrl.href, "_blank");
      },

      /** 还原按钮操作 */
      handleRestore(row){
        const surveyIds = row.surveyId || this.ids;
        ElMessageBox.confirm('确定要还原这些问卷吗？','警告',
          {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}
        ).then(async () => {
          await restoreSurvey(surveyIds);
          this.getList();
          this.$message({message: '还原成功',type:'success'});
        }).catch(() => {});
      },
      
      /** 删除按钮操作 */
      handleDelete(row) {
        const surveyIds = row.surveyId || this.ids;
        ElMessageBox.confirm('确定要删除这个问卷吗？','警告',
          {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}
        ).then(async () => {
          await delSurvey(surveyIds);
          this.getList();
          this.$message({message: '删除成功',type:'success'});
        }).catch(() => {});
      },
    },
  };
  </script>
  
  <style scoped>
  .el-button.el-button--small.is-text{
    padding: 0 2px;
    margin: 0;
  }
  </style>
  